// date.js  日期时间格式化
export function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  let o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ("00" + str).substr(str.length);
}

export function str2Date(dateStr, separator) {
  if (!separator) {
    separator = "-";
  }
  let dateArr = dateStr.split(separator);
  let year = parseInt(dateArr[0]);
  let month;
  //处理月份为04这样的情况
  if (dateArr[1].indexOf("0") == 0) {
    month = parseInt(dateArr[1].substring(1));
  } else {
    month = parseInt(dateArr[1]);
  }
  let day = parseInt(dateArr[2]);
  let date = new Date(year, month - 1, day);
  return date;
}

// 判断结束时间是否大于开始时间

export function timeFun(startTimeStr, endTimeStr) {
  const startTime = new Date(startTimeStr);
  const endTime = new Date(endTimeStr);
  // 判断结束时间是否大于开始时间
  if (endTime >= startTime) {
    return true;
  } else {
    return false;
  }
}

// 月份数据处理

export function monthUtil(data) {
  const obj = {};
  data.forEach((v) => {
    obj[v.name] = v.value;
  });
  return obj;
}

/**
 * 验证开始时间和结束时间的合法性
 * @param beginTime 开始时间
 * @param endTime 结束时间
 * @param cb 回调函数，用于返回验证结果
 */
export function authDate(beginTime, endTime, cb) {
  let obj = {
    type: true,
    msg: "",
  };
  if ((beginTime && !endTime) || (!beginTime && endTime)) {
    obj = {
      type: false,
      msg: "请填写开始日期或者结束日期哦",
    };
  }
  if (beginTime && endTime) {
    if (!timeFun(beginTime, endTime)) {
      obj = {
        type: false,
        msg: "结束时间必须大于开始时间",
      };
    }
  }
  cb(obj);
}

export function getDaysDifferenceAndCheck(startDate, endDate) {
  var start = new Date(startDate);
  var end = new Date(endDate);
  var timeDiff = Math.abs(end - start);
  var daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
  // 判断天数差值是否大于31天
  var isMoreThan31Days = daysDiff > 32;

  return isMoreThan31Days;
}

//判断固定的时间格式 是否大于当前时间
export function isGreaterThanCurrentTime(time) {
  const currentTime = new Date();
  const targetTime = new Date(time);
  return targetTime > currentTime;
}

export function formatDateYear(date) {
  const day = date.getDate().toString().padStart(2, "0");
  const month = (date.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的
  const year = date.getFullYear();
  const hours = date.getHours().toString().padStart(2, "0");
  const minutes = date.getMinutes().toString().padStart(2, "0");
  const seconds = date.getSeconds().toString().padStart(2, "0");

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

//获取现在的时间
export function getNowTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0");
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

//事件过滤操作
export const pickerOptions = {
  disabledDate(time) {
    return time.getTime() > Date.now();
  },
  shortcuts: [
    {
      text: "今天",
      onClick(picker) {
        picker.$emit("pick", new Date());
      },
    },
    {
      text: "昨天",
      onClick(picker) {
        const date = new Date();
        date.setTime(date.getTime() - 3600 * 1000 * 24);
        picker.$emit("pick", date);
      },
    },
    {
      text: "一周前",
      onClick(picker) {
        const date = new Date();
        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
        picker.$emit("pick", date);
      },
    },
    {
      text: "一年前",
      onClick(picker) {
        const date = new Date();
        date.setFullYear(date.getFullYear() - 1);
        picker.$emit("pick", date);
      },
    },
    {
      text: "两年前",
      onClick(picker) {
        const date = new Date();
        date.setFullYear(date.getFullYear() - 2);
        picker.$emit("pick", date);
      },
    },
    {
      text: "三年前",
      onClick(picker) {
        const date = new Date();
        date.setFullYear(date.getFullYear() - 3);
        picker.$emit("pick", date);
      },
    },
  ],
};
